package com.efast.cafe.commponent.log.ext;

import java.time.LocalDateTime;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.event.Level;
import com.efast.cafe.commponent.log.entity.BusLogInfo;
import com.efast.cafe.commponent.log.queue.BusLogQueue;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.date.DateUtils;
import com.efast.cafe.util.date.LocalDateUtils;


public class CafeLoggerFactory implements CafeLogger {

	private final Logger logger;
	
	private final String category;

	// 以下为自定义的日志级别
	//public static final Level PAY_LEVEL = CustomerLevel.BUSINESS;


	private CafeLoggerFactory(Class<?> clazz) {
		logger = LoggerFactory.getLogger(clazz);
		category = "BUSINESS";
	}
	
	private CafeLoggerFactory(Class<?> clazz,String category) {
		logger = LoggerFactory.getLogger(clazz);
		this.category = category;
	}

	public static CafeLoggerFactory getLogger(final Class<?> clazz) {
		return new CafeLoggerFactory(clazz);
	}
	
	public static CafeLoggerFactory getLogger(final Class<?> clazz,String category) {
		return new CafeLoggerFactory(clazz,category);
	}

	
	
	public void cacheLog(Level level, String message, String keyword){
		level = level==null?Level.INFO:level;
		BusLogQueue.getInstance().push(getLogInfoBean(level,message,keyword));
	}
	
	private BusLogInfo getLogInfoBean(Level level,String message,String remark){
		BusLogInfo info = new BusLogInfo();
		info.setCompanyCode(MDC.get("companyCode"));
		info.setCreateTime(LocalDateUtils.dateTime2Str(LocalDateTime.now(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
		info.setLevel(level.toString());
		info.setMsg(message);
		info.setRemark(remark);
		info.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		info.setSystemNo(ConfigUtil.getSystemIndentifier());
		info.setSystemVersion(ConfigUtil.getSystemVersion());
		info.setServiceName(ConfigUtil.getValue("spring.application.name"));
		info.setUserName(MDC.get("userName"));
		info.setClientIp(MDC.get("clientIP"));
		info.setCategory(category);
		return info;
	}

	public void info(String message) {
		logger.info(message);
		
	}

	public void info(String message, Throwable t) {
		logger.info(message, t);
	}

	public void debug(String message) {
		logger.debug(message);
	}

	public void debug(String message, Throwable t) {
		logger.debug(message, t);
	}

	public void warn(String message) {
		logger.warn(message);
	}

	public void warn(String message, Throwable t) {
		logger.warn(message, t);
	}

	public void error(String message) {
		logger.error(message);
	}

	public void error(String message, Throwable t) {
		logger.error(message, t);
	}

	public void trace(String message) {
		logger.trace(message);
	}

	public void trace(String message, Throwable t) {
		logger.trace(message, t);
	}

	


	public void info(String message, Object... params) {
		logger.info(message, params);
	}

	public void debug(String message, Object... params) {
		logger.debug(message, params);
	}

	public void warn(String message, Object... params) {
		logger.warn(message, params);
	}

	public void error(String message, Object... params) {
		logger.error(message, params);
	}

	public void trace(String message, Object... params) {
		logger.trace(message, params);
	}

	
}
